﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace TSGuards
{
    public partial class Setting : Form
    {
        private clsDB DB;
        private NetworkTime NTP= new NetworkTime();
        private bool passchange;
        private int countertick = 0;

        [StructLayout(LayoutKind.Sequential)]
        public struct SYSTEMTIME
        {
            public short wYear;
            public short wMonth;
            public short wDayOfWeek;
            public short wDay;
            public short wHour;
            public short wMinute;
            public short wSecond;
            public short wMilliseconds;
        }

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool SetSystemTime(ref SYSTEMTIME st);



        public Setting()
        {
            InitializeComponent();
            DB = new clsDB();
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            this.ShowInTaskbar = true;
            this.Show();
            notifyIcon1.Visible = false;

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            this.ShowInTaskbar = false;
            this.Hide();
            notifyIcon1.Visible = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //DB.dbConnectMDB(@"D:\PROJECT - Andramatin Architect TimeSheet\Andramatin\att2000.mdb");
                DB.writeLog("TransferUserSchedule Start");
                DB.TransferUserSchedule();
                DB.writeLog("TransferUserSchedule End");
                DB.writeLog("TransferSchedule Start");
                DB.TransferSchedule();
                DB.writeLog("TransferSchedule End");
                DB.writeLog("TransferMasterUserData Start");
                DB.TransferMasterUserData();
                DB.writeLog("TransferMasterUserData End");
                DB.writeLog("TransferCheckTrxData Start");
                DB.TransferCheckTrxData();
                DB.writeLog("TransferCheckTrxData End");
                //DB.dbDisconnectMDB();
                MessageBox.Show("Success pushing data to database server", "Push Data", MessageBoxButtons.OK);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"Error Occured",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnLovDbFile_Click(object sender, EventArgs e)
        {
            txtDbFile.ReadOnly = false;
            DialogResult drDbFile = dlgOpenDbFile.ShowDialog();
            if (drDbFile == DialogResult.OK)
            {
                txtDbFile.Text = dlgOpenDbFile.FileName;
            }
            txtDbFile.ReadOnly = true;
        }

        private void Setting_Load(object sender, EventArgs e)
        {
            try
            {
                txtDbFile.ReadOnly = false;
                txtDbFile.Text = Properties.Settings.Default.mdbfilename;
                txtDbFile.ReadOnly = true;
                txtTMDB.Text = Properties.Settings.Default.mysqldatabase;
                txtTMServer.Text = Properties.Settings.Default.mysqlserver;
                txtTMUser.Text = Properties.Settings.Default.mysqluser;
                txtTMPass.Text = Properties.Settings.Default.mysqlpassword == "" ? "********" : Properties.Settings.Default.mysqlpassword;
                passchange = false;
                synctime.Value = Properties.Settings.Default.synctime;
                if (Properties.Settings.Default.synctime > 60)
                {
                    timer.Interval = Properties.Settings.Default.synctime * 1000;
                }
                else
                {
                    timer.Interval = 60 * 1000;
                }
                countertick = 0;

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error Occured", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Properties.Settings.Default.mdbfilename = txtDbFile.Text;
                Properties.Settings.Default.mysqlserver = txtTMServer.Text;
                Properties.Settings.Default.mysqldatabase = txtTMDB.Text;
                Properties.Settings.Default.mysqluser = txtTMUser.Text;
                if (passchange)
                {
                    Properties.Settings.Default.mysqlpassword = txtPassHide.Text;
                }
                Properties.Settings.Default.synctime = (int)synctime.Value;
                Properties.Settings.Default.Save();
                MessageBox.Show("Setting Saved!)");
                DB.writeLog("Setting Saved");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error Occured", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void txtTMPass_KeyPress(object sender, KeyPressEventArgs e)
        {
            passchange = true;
        }

        private void txtTMPass_Leave(object sender, EventArgs e)
        {
            txtPassHide.Text = txtTMPass.Text;
        }

        private void settingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            this.ShowInTaskbar = true;
            this.Show();
            notifyIcon1.Visible = false;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //getNTPTime();
        }

        private void getNTPTime()
        {
            try
            {
                SYSTEMTIME st = new SYSTEMTIME();
                DateTime netdate = NTP.GetDateTime(false);
                st.wYear = (short)netdate.Year; // must be short
                st.wMonth = (short)netdate.Month;
                st.wDay = (short)netdate.Day;
                st.wHour = (short)netdate.Hour;
                st.wMinute = (short)netdate.Minute;
                st.wSecond = (short)netdate.Millisecond;
                SetSystemTime(ref st); // invoke this method.
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error Occured", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
           // DateTime DT = new NTP.GetDateTime(false);
//            MessageBox.Show(NTP.GetDateTime(false).ToString());
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            try
            {
                StatusLabel.Text = "Syncing time...";
                //getNTPTime();
                StatusLabel.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error Occured", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            try
            {
                countertick++;
                StatusLabel.Text = "Transfering check in out data...";
                DB.TransferCheckTrxData();
                StatusLabel.Text = "";
                if (countertick > 10)
                {
                    countertick = 0;
                    StatusLabel.Text = "Transfering user schedule...";
                    DB.TransferUserSchedule();
                    StatusLabel.Text = "Transfering schedule data...";
                    DB.TransferSchedule();
                    StatusLabel.Text = "Transfering master user data...";
                    DB.TransferMasterUserData();
                    StatusLabel.Text = "";
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error Occured", MessageBoxButtons.OK, MessageBoxIcon.Error);
            } 
        }
    }
}
